package br.com.simulador.session;

import java.util.Arrays;

import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

import org.jboss.seam.annotations.In;
import org.jboss.seam.annotations.Name;
import org.jboss.seam.framework.EntityQuery;

import br.com.simulador.model.Questao;

@Name("questaoList")
public class QuestaoList extends EntityQuery<Questao> {
	
	@In
	EntityManager entityManager;

	private static final String EJBQL = "select questao from Questao questao";
	
	private static final String EJBQLID = "select questao from Questao questao where id=:id";

	private static final String[] RESTRICTIONS = { "lower(questao.descricao) like lower(concat(#{questaoList.questao.descricao},'%'))", };

	private Questao questao = new Questao();

	public QuestaoList() {
		setEjbql(EJBQL);
		setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
		setMaxResults(25);
	}

	public Questao getQuestao() {
		return questao;
	}
	
	public Questao getByID(Long id){
		try{
		return (Questao) entityManager.createQuery(EJBQLID)
				.setParameter("id", id)
				.getSingleResult();
		}
		catch(NoResultException e){
			return null;
		}
	}
}
